/**
 *------------------------------------------------------------------------------
 *  @copyright Copyright (c) 2022  <dx_65535@163.com>. 
 * 
 *  @file       : event_handler.cc
 *  @author     : Xiang.D (dx_65535@163.com)
 *  @version    : 1.0
 *  @brief      : Blog: https://linuxtaoist.gitee.io
 *  @date       : 2022-06-12
 * 
 * 
 *  Change History:
 *  <Date>     | <Version> | <Author>       | <Description>
 *------------------------------------------------------------------------------
 *  2022/06/12 | 1.0.0.1   | Xiang.D        | Create file
 *------------------------------------------------------------------------------
 * 
 */

#include <stdio.h>
#include "message_epoll.h"
#include "event_handler.h"

#define ELOGE(fmt, args...)  printf("%d EventHandler E: " fmt, __LINE__, ##args)

#ifdef TEST_DEBUG
#define ELOGD(fmt, args...)  printf("%d EventHandler D: " fmt, __LINE__, ##args)
#else
#define ELOGD(fmt, args...)
#endif

CMsgEpoll *pMsgPoll = nullptr;

CEventHandler::CEventHandler()
{

}

CEventHandler::~CEventHandler()
{

}

int CEventHandler::Init()
{
    pMsgPoll = CMsgEpoll::GetInstance();

    if (pMsgPoll) {
        pMsgPoll->Init();
    } else {
        ELOGE("pMsgPoll is NULL!\n");
    }

    return 0;
}

int CEventHandler::Exit()
{
    if (pMsgPoll) {
        pMsgPoll->Exit();
    } else {
        ELOGE("pMsgPoll is NULL!\n");
    }

    return 0;
}

void CEventHandler::Run()
{
    if (pMsgPoll) {
        pMsgPoll->Run();
    } else {
        ELOGE("pMsgPoll is NULL!\n");
    }
}

int CEventHandler::RegisterEvent(int fd, ETransChannel channel, CallbackPtr cb)
{
    if (fd < 0) {
        ELOGE("fd is bad file descriptor.\n");
    }

    if (pMsgPoll) {
        pMsgPoll->AddPoll(fd, channel, cb);
    } else {
        ELOGE("pMsgPoll is NULL!\n");
        return -1;
    }

    return 0;
}

int CEventHandler::UnregisterEvent(int fd)
{
    if (pMsgPoll) {
        pMsgPoll->DelPoll(fd);
    } else {
        ELOGE("pMsgPoll is NULL!\n");
        return -1;
    }

    return 0;
}

